﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using System.Data;
using System.Text;
using System.Security.Cryptography;

namespace Simple.Services;

public class QHSEXyViolationTypeService
{
    private readonly SimpleDbContext _context;
    private readonly CacheService _cacheService;
    private const string SecretKey = "YourSecretKey";
    public QHSEXyViolationTypeService(SimpleDbContext context, CacheService cacheService)
    {
        _context = context;
        _cacheService = cacheService;
    }
    public async Task<List<QHSEXyViolationTypeModel>> GetAsync()
    {
        string name = "947";
        var result =await  _cacheService.GetQHSEViolationTypeAsync(name);
        if(result.Count ==0)
        {
            var violationTypes = await _context.Set<QHSESysXyViolationType>().AsNoTracking().OrderBy(u=>u.OrderNo).ToListAsync();
            var models = MapperHelper.Map<List<QHSEXyViolationTypeModel>>(violationTypes);

            await  _cacheService.SetQHSEViolationTypeAsync(models,name);
            return models;
        }
        return result;
    }
    public async Task<List<QHSEXyViolationTypeModel>> GetAsync(SimpleDbContext context)
    {
        string name = "947";
        var result = await _cacheService.GetQHSEViolationTypeAsync(name);
        if (result.Count == 0)
        {
            var violationTypes = await context.Set<QHSESysXyViolationType>().AsNoTracking().OrderBy(u => u.OrderNo).ToListAsync();
            var models = MapperHelper.Map<List<QHSEXyViolationTypeModel>>(violationTypes);

            await _cacheService.SetQHSEViolationTypeAsync(models, name);
            return models;
        }
        return result;
    }
    public async Task<QHSEXyViolationTypeModel> GetByCode(string code)
    {
        var violationType =await _context.Set<QHSESysXyViolationType>().AsNoTracking().Where(u=>u.ViolationTypeCode == code).FirstOrDefaultAsync();
        var violationTypeModel = MapperHelper.Map<QHSEXyViolationTypeModel>(violationType);
        return violationTypeModel;
    }
    public bool ValidateSignature(long timestamp, string sign)
    {
        // 计算签名
        var calculatedSign = CalculateSignature(timestamp);

        // 将计算得到的签名与接收到的签名进行比较
        return calculatedSign == sign.ToLower();
    }

    private string CalculateSignature(long timestamp)
    {
        // 构造要进行MD5计算的字符串
        var concatenatedString = $"{timestamp}-{SecretKey}";

        // 计算第一次MD5哈希
        using (var md5 = MD5.Create())
        {
            var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(concatenatedString));

            // 将字节数组转换为十六进制字符串
            var firstHash = BitConverter.ToString(hash).Replace("-", "").ToLower();

            // 进行第二次MD5哈希
            var secondHash = md5.ComputeHash(Encoding.UTF8.GetBytes(firstHash));

            // 将第二次MD5哈希的字节数组转换为十六进制字符串
            return BitConverter.ToString(secondHash).Replace("-", "").ToLower();
        }
    }

}
